﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;


namespace Common
{
    public class ScriptHelper
    {
        // Methods
        public static void Alert(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');</script>");
        }

        public static void AlertBack(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');history.back();</script>");
        }

        public static void AlertClose(string msg)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');window.close();</script>");
        }

        public static void AlertRedirect(string msg, string url)
        {
            HttpContext.Current.Response.Write("<script>alert('" + msg + "');location.href=('" + url + "')</script>");
        }

        public static void Alert(Page page, Control control, string msg)
        {
            StringBuilder js = new StringBuilder();
            js.AppendFormat("alert('{0}');", msg);
            ScriptManager.RegisterStartupScript(control, page.GetType(), Guid.NewGuid().ToString(), js.ToString(), true);
        }

        public static void AlertBack(Page page, Control control, string msg)
        {
            StringBuilder js = new StringBuilder();
            js.AppendFormat("alert('{0}');history.back();", msg);
            ScriptManager.RegisterStartupScript(control, page.GetType(), Guid.NewGuid().ToString(), js.ToString(), true);
        }

        public static void AlertClose(Page page, Control control, string msg)
        {
            StringBuilder js = new StringBuilder();
            js.AppendFormat("alert('{0}');window.close();", msg);
            ScriptManager.RegisterStartupScript(control, page.GetType(), Guid.NewGuid().ToString(), js.ToString(), true);
        }

        public static void AlertRedirect(Page page, Control control, string msg, string url)
        {
            StringBuilder js = new StringBuilder();
            js.AppendFormat("alert('{0}');location.href=('{1}');", msg, url);
            ScriptManager.RegisterStartupScript(control, page.GetType(), Guid.NewGuid().ToString(), js.ToString(), true);
        }

        public static string GetRand(int length, bool IsNum)
        {
            char[] chArray = new char[] { 
            'A', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'N', 'P', 'Q', 'R', 'T', 'U', 
            'X', 'Y', 'Z', '2', '3', '4', '6', '7', '8', '9'
         };
            char[] chArray2 = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            char[] chArray3 = new char[0];
            if (IsNum)
            {
                chArray3 = chArray2;
            }
            else
            {
                chArray3 = chArray;
            }
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                builder.Append(chArray3[random.Next(chArray3.Length)]);
            }
            return builder.ToString();
        }

        public static string IncScript(string str)
        {
            string str2 = "<script";
            return (str2 + " language=\"javascript\" type=\"text/javascript\">\r\n" + str + "\r\n</script>");
        }

        public static string RemoveHTML(string str)
        {
            string str2 = str;
            return Regex.Replace(str, "<[^>]+>", "");
        }

        public static void ShowAlertAndBackHistory(Page page, string msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("history.back();", new object[0]);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        public static void ShowAlertAndCloseRefreshScript(Page page, string msg, string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.close();", new object[0]);
            if (url != "")
            {
                builder.AppendFormat("opener.location.href='{0}';", url);
            }
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        public static void ShowAlertAndCloseRefreshScript(WebControl Control, string msg, string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.close();", new object[0]);
            if (url != "")
            {
                builder.AppendFormat("opener.location.href='{0}';", url);
            }
            builder.Append("</script>");
            Control.Attributes.Add("onclick", builder.ToString());
        }

        public static void ShowAlertAndCloseScript(Page page, string msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.close();", new object[0]);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        public static void ShowAlertAndCloseScript(WebControl Control, string msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.close();", new object[0]);
            builder.Append("</script>");
            Control.Attributes.Add("onclick", builder.ToString());
        }

        public static void ShowAlertAndRedirectScript(Page page, string msg, string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        public static void ShowAlertAndTopRedirectScript(Page page, string msg, string url)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("top.location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", builder.ToString());
        }

        public static void ShowAlertScript(Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        public static void ShowCloseScript(Page page)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>window.close();</script>");
        }

        public static void ShowCloseScript(WebControl control)
        {
            control.Attributes.Add("onclick", "window.close();");
        }

        public static void ShowConfirmScript(WebControl control, string msg)
        {
            control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        public static void ShowCustomScript(Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");
        }

        public static void ShowOpenMaxScript(WebControl control, string url)
        {
            control.Attributes.Add("onclick", "var mywin=window.open('" + url + "');mywin.moveTo(0,0);mywin.resizeTo(screen.availWidth,screen.availHeight);");
        }

        public static void ShowOpenScript(WebControl control, string url)
        {
            control.Attributes.Add("onclick", "window.open('" + url + "');");
        }

        public static void ShowOpenScript(WebControl control, string url, int width, int height)
        {
            control.Attributes.Add("onclick", string.Concat(new object[] { "window.open('", url, "', '', 'width=", width, ",height=", height, ",fullscreen=no, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no');" }));
        }

        public static string ToHtml(string txt)
        {
            txt = txt.Replace("\r\n","<br/>");
            return txt;
        }


        ///   <summary>   
        ///   返回两个日期之间的时间间隔（y：年份间隔、M：月份间隔、d：天数间隔、h：小时间隔、m：分钟间隔、s：秒钟间隔、ms：微秒间隔）   
        ///   </summary>   
        ///   <param   name="Date1">开始日期</param>   
        ///   <param   name="Date2">结束日期</param>   
        ///   <param   name="Interval">间隔标志（y：年份间隔、M：月份间隔、d：天数间隔、h：小时间隔、m：分钟间隔、s：秒钟间隔、ms：微秒间隔）</param>   
        ///   <returns>返回间隔标志指定的时间间隔</returns>   
        public static int DateDiff(System.DateTime Date1, System.DateTime Date2, string Interval)
        {
            double dblYearLen = 365;//年的长度，365天   
            double dblMonthLen = (365 / 12);//每个月平均的天数   
            System.TimeSpan objT;
            objT = Date2.Subtract(Date1);
            switch (Interval)
            {
                case "y"://返回日期的年份间隔   
                    return System.Convert.ToInt32(objT.Days / dblYearLen);
                case "M"://返回日期的月份间隔   
                    return System.Convert.ToInt32(objT.Days / dblMonthLen);
                case "d"://返回日期的天数间隔   
                    return objT.Days;
                case "h"://返回日期的小时间隔   
                    return objT.Hours;
                case "m"://返回日期的分钟间隔   
                    return objT.Minutes;
                case "s"://返回日期的秒钟间隔   
                    return objT.Seconds;
                case "ms"://返回时间的微秒间隔   
                    return objT.Milliseconds;
                default:
                    break;
            }
            return 0;
        }
    }


}
